/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sun.ShipperProcessor;

import com.sun.Entity.Shippingorder;
import com.sun.Facade.ShippingorderFacadeLocal;
import exceptions.OrderAlreadyShippedException;
import exceptions.OrderAlreadyTakenException;
import exceptions.OrderWaitingForShippingException;
import exceptions.UnknownOrderException;
import javax.ejb.EJB;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.ejb.Stateless;
import javax.jws.WebParam;

/**
 *
 * @author Trasheur
 */
@WebService()
@Stateless()
public class TakeOrderWS {

    @EJB
    private ShippingorderFacadeLocal shippingorderFacade;

    /**
     * Web service operation
     */
    @WebMethod(operationName = "TakeOrder")
    public Boolean TakeOrder(@WebParam(name = "OrderID") String OrderID)
            throws
            UnknownOrderException,
            OrderAlreadyTakenException,
            OrderAlreadyShippedException,
            OrderWaitingForShippingException {

        // Verification OrderID existance
        Shippingorder Order = shippingorderFacade.find(OrderID);
        if (Order == null) {
            throw new UnknownOrderException();
        }

        if (Order.getShippeddate() != null) {
            throw new OrderAlreadyShippedException();
        }
        
        if (Order.getOrdertaken() == 1) {
            throw new OrderAlreadyTakenException();
        }    

        Order.setOrdertaken(1);
        shippingorderFacade.edit(Order);

        return true;
    }
}
